package com.wohuijituan.whcsmerchant.utils;

import android.view.View;

import com.wohuijituan.whcsmerchant.bean.GoodsCategoryABCData;
import com.wohuijituan.whcsmerchant.bean.GoodsCategoryBCData;
import com.wohuijituan.whcsmerchant.bean.GoodsCategoryData;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/30 16:43
 * 版本：V1.0
 * 修改历史：
 */
public class MyUtile {
    /**
     * 将ip的整数形式转换成ip形式
     *
     * @param ipInt
     * @return
     */
    public static String intToIp(int ipInt) {
        StringBuilder sb = new StringBuilder();
        sb.append(ipInt & 0xFF).append(".");
        sb.append((ipInt >> 8) & 0xFF).append(".");
        sb.append((ipInt >> 16) & 0xFF).append(".");
        sb.append((ipInt >> 24) & 0xFF);
        return sb.toString();
    }
    /**
     * 把网络请求的数据进行分类
     */
    public static List<GoodsCategoryABCData> turnToABC(List<GoodsCategoryData> datas) {
        List<GoodsCategoryData> categoryListA = new ArrayList<>();
        List<GoodsCategoryData> categoryListB = new ArrayList<>();
        List<GoodsCategoryData> categoryListC = new ArrayList<>();
        List<GoodsCategoryBCData> categoryListBC = new ArrayList<>();
        List<GoodsCategoryABCData> categoryListABC = new ArrayList<>();
        for(GoodsCategoryData data:datas){//先份三组分别对应一级二级三级分类
            if(data.getCC_Level()==1){
                categoryListA.add(data);

            }else if (data.getCC_Level()==2){
                categoryListB.add(data);
            }else if (data.getCC_Level()==3){
                categoryListC.add(data);
            }
        }
        LogUtils.e(categoryListA.size()+"**"+categoryListB.size()+"**"+categoryListC.size());
        for (GoodsCategoryData data:categoryListB){//把二级列表给goodsSortListBC
            GoodsCategoryBCData bcData = new GoodsCategoryBCData();
            bcData.setCC_GUID(data.getCC_GUID());
            bcData.setCC_Name(data.getCC_Name());
            bcData.setCC_ParentGUID(data.getCC_ParentGUID());
            bcData.setCC_Icon(data.getCC_Icon());
            bcData.setCC_Level(data.getCC_Level());
            bcData.setCC_Sort(data.getCC_Sort());
            bcData.setCC_Count(data.getCC_Count());
            List<GoodsCategoryData> list = new ArrayList<>();
            bcData.setCategoryCList(list);
            categoryListBC.add(bcData);
        }
        LogUtils.e(categoryListBC.size() + "**");
        for (GoodsCategoryData data:categoryListC){//继续把二级三级分类合为一个组
            String numberParent = data.getCC_ParentGUID();
            for (GoodsCategoryBCData bcData:categoryListBC){
                if (numberParent.equals(bcData.getCC_GUID())){
                    LogUtils.e("2222222222");
                    bcData.getCategoryCList().add(data);
                    break;
                }
            }
        }

        LogUtils.e(categoryListBC.size() + "**");
        for (GoodsCategoryData data:categoryListA){//把一级列表给goodsSortListABC
            GoodsCategoryABCData abcData = new GoodsCategoryABCData();
            abcData.setCC_GUID(data.getCC_GUID());
            abcData.setCC_Name(data.getCC_Name());
            abcData.setCC_ParentGUID(data.getCC_ParentGUID());
            abcData.setCC_Icon(data.getCC_Icon());
            abcData.setCC_Level(data.getCC_Level());
            abcData.setCC_Sort(data.getCC_Sort());
            abcData.setCC_Count(data.getCC_Count());
            List<GoodsCategoryBCData> list = new ArrayList<>();
            abcData.setCategoryBCList(list);
            categoryListABC.add(abcData);
        }
        LogUtils.e(categoryListABC.size() + "**");
        for (GoodsCategoryBCData data:categoryListBC){

            String numberParent = data.getCC_ParentGUID();
            for (GoodsCategoryABCData abcData:categoryListABC){

                if (numberParent.equals(abcData.getCC_GUID())){

                    abcData.getCategoryBCList().add(data);
                    break;
                }
            }
        }
        LogUtils.e(categoryListABC.size() + "**");
        return categoryListABC;
    }

    /**
     *
     * @param startWord 加密字符串
     * @param endWord 加密字符串
     * @param strs 需要加密的参数
     * @return
     */
    public static String getEncodeSign(String startWord,String endWord,String... strs){
        String sign = startWord;
        for (String str:strs){
            sign = sign+str;
        }
        sign = sign + endWord;
        sign = MD5Encoder.encode(sign);
        return sign;
    }
    /**
     * 获取控件的高度或者宽度  isHeight=true则为测量该控件的高度，isHeight=false则为测量该控件的宽度
     * @param view
     * @param isHeight
     * @return
     */
    public static int getViewHeight(View view, boolean isHeight){
        int result;
        if(view==null)return 0;
        if(isHeight){
            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            view.measure(h,0);
            result =view.getMeasuredHeight();
        }else{
            int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            view.measure(0,w);
            result =view.getMeasuredWidth();
        }
        return result;
    }
}
